package com.cat.binaryTree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/n-ary-tree-level-order-traversal/
 * @create 2025/9/30 20:36
 * @since JDK17
 */

public class Solution97 {
    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };
    List<List<Integer>> ans = new ArrayList<>();
    void dfs(Node root, int k) {
        if (root == null) {
            return;
        }
        if (ans.size() <= k) {
            ans.add(new ArrayList<>());
        }
        ans.get(k).add(root.val);
        for (var child : root.children) {
            dfs(child, k + 1);
        }
    }

    public List<List<Integer>> levelOrder(Node root) {
        dfs(root, 0);
        return ans;
    }
}
